<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../rurple.css" type="text/css" />

<title>Globalization is a bad thing</title>
</head>
<body>
<h2 class="title">31. <span class="pykeyword">global</span>ization is a bad thing.</h2>

<p>Suppose you are writing a long program together with a friend.  Because you have learned good programming practices, you are always trying to use descriptive names for your variables.  But then you start thinking: <i>What if my friend uses the same name as I use for one of her variables?  If I make a change to the value of my variable, will it change hers in an unwanted way?</i></p>

<p>Python has ways to help you avoid this kind of problem by the use of <i>namespaces</i>.  This prevents variable that we want to use in a small part of a program to affect other parts.  We will start learning about namespaces in this lesson.</p>

<!--==============================-->
<hr class="line" />

<h3 class="section">Locally defined variables.</h3>

<p>Here's a simple example to try:</p>

<pre>
>>> a = 1
>>> <span class="keyword">def</span> my_function():
...     a = 3
...     <span class="keyword">print</span> a
...    

>>> my_function()
3

>>> <span class="keyword">print</span> a
1
</pre>

<p>Are you surprised that the value of <tt>a</tt> did not change?  Here's what's happened.</p>

<p>You started by defining a variable names "<tt>a</tt>" with its value set to 1.  Then, inside a function, you "apparently" changed the value of "<tt>a</tt>" to 3.  Actually, what you did was to create a <u>new</u> variable "<tt>a</tt>" inside the function, and gave it a value of 3.  The value of the other variable "<tt>a</tt>" was left unchanged.</p>

<p>Python automatically creates a <i>namespace</i> inside each function, where the names of variables defined take a unique value, independent of how other variables with the same name outside the function might be defined.  A namespace is like a little notebook in which Python keeps track of all the variable names.  It is as though Python create a unique notebook for each function, so that you don't have to worry if other variables with the same name exist elsewhere.</p>

<p>The variables used inside a function are no longer defined when the function ends; we say that they are <i>out of scope</i>.</p>

<pre>
>>> <span class="keyword">def</span> simple_function():
...     local_variable = <span class="string">"I'm here!"</span>
...     <span class="keyword">print</span> local_variable
...    
>>> simple_function()
I'm here!

>>> <span class="keyword">print</span> local_variable
Traceback (most recent call last):
  File "&lt;input>", line 1, in ?
NameError: name <span class="string">'local_variable'</span> is not defined
</pre>

<p>This namespace trick is nice, but what if we want to change the value of a variable inside a function?  For example, suppose we want to keep track of how many times we are calling a certain function.  We can do that, using the Python keyword <span class="pykeyword">global</span>.</p>

<pre>
>>> number_of_times = 0
>>> <span class="keyword">def</span> two_year_old_child():
...     <span class="keyword">global</span> number_of_times
...     number_of_times += 1
...     <span class="keyword">print</span> <span class="string">"Why?     %s times"</span> % number_of_times
...    
>>> two_year_old_child()
Why?          1 times <span class="comment"># I know, it should be "1 time", without a final 's'</span>

>>> two_year_old_child()
Why?          2 times

>>> two_year_old_child()
Why?          3 times

>>> <span class="keyword">print</span> number_of_times
3
</pre>

<p>By using the keyword <span class="pykeyword">global</span>, we tell Python <u>not</u> to create a new version of the variable locally inside the function, but rather use the variable with that name "outside", in the so-called "global" environment.</p>

<p>You should always try to find ways to avoid using <span class="pykeyword">global</span> variables if you can, as they can introduce unwanted changes in other parts of the program.  In programming, <span class="pykeyword">global</span>ization is usually a bad thing.</p>

<!--==============================-->
<hr class="line" />

<h3 class="section">Out of scope</h3>

<p>Larry and Curly are walking merrily together.  Suddenly, Moe sees his friends from a distance and starts running towards them.   Just as Moe finally catches up with Larry and Curly, he goes out of scope and disappears from the screen. If you want for him to keep up with his friends, you need to remove three <span class="comment">#</span> symbols.</p>

<pre>
Larry = UsedRobot(colour=<span class="string">'blue'</span>)
Curly = UsedRobot(1, 3, colour=<span class="string">'green'</span>)

<span class="comment"># Using two keywords you haven't seen yet!</span>
<span class="keyword">for</span> i <span class="keyword">in</span> range(3):
    Larry.move()
    Curly.move()

<span class="keyword">def</span> wait_for_me():
    <span class="comment">#global Moe</span>
    Moe = UsedRobot(1, 2, colour=<span class="string">'yellow'</span>)
    <span class="comment"># Using two same new keywords again!</span>
    <span class="keyword">for</span> i <span class="keyword">in</span> range(3):
        Moe.move()
        Larry.move()
        Moe.move()
        Curly.move()

wait_for_me()
<span class="comment"># first image below; Moe is there.</span>
Larry.move()
<span class="comment"># second image below; Moe is gone.</span>
Curly.move()
<span class="comment">#Moe.move()</span>
Larry.move()
Curly.move()
<span class="comment">#Moe.move()</span>
Larry.turn_off()
</pre>

<p><img alt="Moe there"  src="../../images/inter/scope1.png" /></p>

<p><img alt="Moe gone"  src="../../images/inter/scope2.png" /></p>

<!--==============================-->
<hr class="line" />

<h3 class="try">Try it!</h3>

<p>Try this program on your own, in Reeborg's world, and then change it so that Moe can keep up with his friends!</p>

<div class="lessons_nav">
<a href="30-dot.htm"><img alt="previous" src=
"../../images/previous.png" />Object-Oriented Programming: "dot" notation.</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png" /></a> - <a href=
"32-return.htm">Many returns <img alt="next"
src="../../images/next.png" /></a>
</div>
</body>
</html>
